﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WPToolkit.Resources;
using System.IO.IsolatedStorage;
using System.IO;
namespace WPToolkit
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings;
            if(setting.Contains("txtValue"))
                txtLoadStage.Text = (String) setting["txtValue"];
            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
            if (file.FileExists("notepad.txt")) {
                IsolatedStorageFileStream stream = file.OpenFile("notepad.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read);
                StreamReader reader = new StreamReader(stream);
                txtNotePad.Text =  reader.ReadToEnd();
                reader.Close();
            }

        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings;
            setting["txtValue"] = txtLoadStage.Text;
        }

        private void btnSaveStage_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings;
            setting["txtValue"] = txtLoadStage.Text;
            setting.Save();
        }

        private void btnSaveFile_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream stream = file.CreateFile("notepad.txt");
            StreamWriter writer = new StreamWriter(stream);
            writer.Write(txtNotePad.Text);
            writer.Flush();
            writer.Close();
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}